package TUILayer;
import java.util.Scanner;
import ControlLayer.AddressCtr;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;

    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
    }
    
    public void start()
    {
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   //show person
              getPerson();
          }
          else{
              if(choice == 2){
                    addPerson();
              }  
              else{
                  if(choice == 3){
                      deletePerson();
                  }  
                  else{
                      if(choice == 4){
                          updatePersonMenu();
                      }
                      else{
                          if(choice == 5){
                              listAllPersons();
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    public void updatePersonMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choice = writeUpdatePersonMenu(); 
          if(choice == 1)
          {
              updatePersonName();
          }
          else{
              if(choice == 2){
                    updatePersonLastName();
              }  
              else{
                  if(choice == 3){
                      updatePersonPhone();
                  }  
                  else{
                      if(choice == 4){
                          updatePersonAddress();
                      }
                      else{
                          if(choice == 5){
                          updatePersonZip();
                          }
                          else{
                              if(choice == 6){
                                  updatePersonCity();
                              }
                              else{
                              exit = true;
                              }
                           }
                        }
                    }
                }
            }
            
       }
    }
    
    public int writeUpdatePersonMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Update Person Menu ***");
            System.out.println(" (1) Update name");
            System.out.println(" (2) Update last name");
            System.out.println(" (3) Update phone");
            System.out.println(" (4) Update address");
            System.out.println(" (5) Update zip-code");
            System.out.println(" (6) Update city");
            System.out.println(" (7) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
        
    private void waitInput()
    {
        System.out.println("Press enter to continue");
        new Scanner(System.in).nextLine();
    }
        
    public String inputPersonsPhone()
    {   
        boolean ok = false;
        String phone = "0";
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Phone:  ");
           try{
             phone = keyboard.nextLine();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }
        return phone;
    }
    
    public String inputName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write name:  ");
         String name = keyboard.nextLine();
         return name;
    }
    
    public String inputLastName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write last name:  ");
         String lastName = keyboard.nextLine();
         return lastName;
    }
    
    public String inputPhone()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write phone number:  ");
         String phone = keyboard.nextLine();
         return phone;
    }
    
    public String  inputAddress()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write address: ");
        String address = keyboard.nextLine();
        return address;
    }
   
    public String inputZip()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write zip-code:  ");
         String zip = keyboard.nextLine();
         return zip;
    }
    
    public String inputCity()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write city:  ");
         String city = keyboard.nextLine();
         return city;
    }
    
    public String inputNew()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write updated info:  ");
         String newName = keyboard.nextLine();
         return newName;
    }
    
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }

    public void getPerson()
    {
        String phone = inputPersonsPhone();
        
        if (addrCtr.findPerson(phone) == null)
        {
            System.out.println("The phone number does not exist in the address book.");
        }
        else
        {
        String personName = addrCtr.findPerson(phone).getName();
        String personLastName = addrCtr.findPerson(phone).getLastName();
        String personAddress = addrCtr.findPerson(phone).getAddress();
        String personZip = addrCtr.findPerson(phone).getZip();
        String personCity = addrCtr.findPerson(phone).getCity();
        
        System.out.println("INFO ABOUT PERSON WITH PHONE: " +phone);
        System.out.println("Name: " +personName);
        System.out.println("Last name: " +personLastName);
        System.out.println("Address: " +personAddress);
        System.out.println("Zip-code: " +personZip);
        System.out.println("City: " +personCity);
        }
        
        waitInput();
    }   
    
    public void addPerson()
    {
        int sizeOne = addrCtr.listSize();
        
        String name = inputName();
        String lastName = inputLastName();
        String phone = inputPhone();
        String address = inputAddress();
        String zip = inputZip();
        String city = inputCity();
        
        addrCtr.addPerson(name, lastName, phone, address, zip, city);
        
        int sizeTwo = addrCtr.listSize();
        
        if (sizeOne < sizeTwo)
        {
            System.out.println("Person successfully added!");
        }
        else
        {
            System.out.println("Person was not added. Please try again...");
        }
        
        waitInput();
    }
    
    public void deletePerson()
    {
        int sizeOne = addrCtr.listSize();
        
        String phone = inputPhone();
        
        addrCtr.deletePerson(phone);
        
        int sizeTwo = addrCtr.listSize();
        
        if (sizeOne > sizeTwo)
        {
            System.out.println("Person successfully deleted!");
        }
        else
        {
            System.out.println("The phone number does not exist in the address book...");
        }
        
        waitInput();
    }
    
    public void updatePersonName()
    {
        String phone = inputPhone();
        String name = inputName();
        String newName = inputNew();
        
        addrCtr.updatePersonName(phone, name, newName);
        
        System.out.println("Name successfully updated!");
        
        waitInput();
    }
    
    
    public void updatePersonLastName()
    {
        String phone = inputPhone();
        String lastName = inputLastName();
        String newLastName = inputNew();
        
        addrCtr.updatePersonLastName(phone, lastName, newLastName);
        
        System.out.println("Last name successfully updated!");
        
        waitInput();
    }
    
    public void updatePersonPhone()
    {
        String phone = inputPhone();
        String newPhone = inputNew();
        
        addrCtr.updatePersonPhone(phone, newPhone);
        
        
        System.out.println("Phone successfully updated!");
        
        waitInput();
    }
    
    public void updatePersonAddress()
    {
        String phone = inputPhone();
        String address = inputAddress();
        String newAddress = inputNew();
        
        addrCtr.updatePersonAddress(phone, address, newAddress);
        
        System.out.println("Address successfully updated!");
        
        waitInput();
    }
    
    public void updatePersonZip()
    {
        String phone = inputPhone();
        String zip = inputZip();
        String newZip = inputNew();
        
        addrCtr.updatePersonZip(phone, zip, newZip);
        
        System.out.println("Zip-code successfully updated!");
        
        waitInput();
    }
    
    public void updatePersonCity()
    {
        String phone = inputPhone();
        String city = inputCity();
        String newCity = inputNew();
        
        addrCtr.updatePersonName(phone, city, newCity);
        
        System.out.println("City successfully updated!");
        
        waitInput();
    }
    
    public void listAllPersons()
    {
        addrCtr.listAllPersons();
        waitInput();
    }
}






















            
        
